//
//  Find2DevView.m
//  YSSmartHome
//
//  Created by yanghao on 2016/12/5.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "Find2DevView.h"
#import "MQTTSessionManager.h"
#import "ZTJoinDevRecord.h"
#import "DataManager.h"
@interface Find2DevView ()
@property (nonnull, nonatomic, strong) UIView   *mCoverView;
@property (nonnull, nonatomic, strong) UIView   *mBombbox;
@property (nonnull, nonatomic, strong) UIButton *mTileButton;
@property (nonnull, nonatomic, strong) UIView   *mHSplitView;
@property (nonnull, nonatomic, strong) UIView   *mVSplitView;
@property (nonnull, nonatomic, strong) UIButton *mSureButton;
@property (nonnull, nonatomic, strong) UIButton *mCancelButton;
@end
@implementation Find2DevView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.mCoverView];
    [self.mCoverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.mCoverView addSubview:self.mBombbox];
    [self.mBombbox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(kIphoneSize_Widith(214));
        make.center.equalTo(self.mCoverView);
    }];
    [self.mCoverView addSubview:self.mTileButton];
    [self.mTileButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.mBombbox);
        make.top.equalTo(self.mBombbox.top).offset(kIphoneSize_Height(16));
    }];
    [self.mBombbox addSubview:self.mHSplitView];
    [self.mHSplitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mTileButton.bottom).offset(kIphoneSize_Height(kIphoneSize_Height(16)));
        make.leading.trailing.equalTo(self.mBombbox);
        make.height.equalTo(1);
    }];
    [self.mBombbox addSubview:self.mCancelButton];
    [self.mCancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mHSplitView.bottom);
        make.leading.equalTo(self.mBombbox);
        make.bottom.equalTo(self.mBombbox);
        make.width.equalTo(self.mBombbox.width).multipliedBy(0.5);
        make.height.equalTo(kIphoneSize_Height(47));
    }];
    [self.mBombbox addSubview:self.mVSplitView];
    [self.mVSplitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mHSplitView.bottom);
        make.leading.equalTo(self.mCancelButton.trailing);
        make.width.equalTo(1);
        make.bottom.equalTo(self.mBombbox);
    }];
    [self.mBombbox addSubview:self.mSureButton];
    [self.mSureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.mCancelButton);
        make.leading.equalTo(self.mVSplitView.trailing);
        make.trailing.equalTo(self.mBombbox.trailing);
    }];
    [self.mBombbox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mCancelButton.bottom);
    }];
}

#pragma  mark - actions

- (void)cancel {
    
    self.joinDevRecord.isrefuse = true;
    [self removeFromSuperview];
    kMQTTSessionManager.isShowJoinView = false;
}

- (void)sure {
    [kMQTTSessionManager deviceAdd:kMQTTSessionManager.currentUser.currentBindApsn Luid:self.joinDevRecord.luid];
    [self removeFromSuperview];
    [kDataManager.joinDevListenList removeObject:self.joinDevRecord];
     kMQTTSessionManager.isShowJoinView = false;
    
}
#pragma mark - lazy loading
- (UIView *)mCoverView
{
    if (!_mCoverView) {
        _mCoverView = [[UIView alloc]init];
        _mCoverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    }
    return _mCoverView;
}
- (UIView *)mBombbox
{
    if (!_mBombbox) {
        _mBombbox = [[UIView alloc] init];
        _mBombbox.backgroundColor = [UIColor whiteColor];
    }
    return _mBombbox;
}
- (UIButton *)mTileButton
{
    if (!_mTileButton) {
        _mTileButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_mTileButton setImage:[UIImage imageNamed:@"newdevice_icon_find"] forState:UIControlStateNormal];
        _mTileButton.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        _mTileButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        [_mTileButton setTitle:@"是否添加新设备" forState:UIControlStateNormal];
        [_mTileButton setTitleColor:RGBColor(51, 51, 51) forState:UIControlStateNormal];
        _mTileButton.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    return _mTileButton;
}

-(UIView *)mVSplitView
{
    if (!_mVSplitView) {
        _mVSplitView = [[UIView alloc]init];
        _mVSplitView.backgroundColor = kSplitLineColor;
    }
    return _mVSplitView;
}

-(UIView *)mHSplitView
{
    if (!_mHSplitView) {
        _mHSplitView = [[UIView alloc]init];
        _mHSplitView.backgroundColor = kSplitLineColor;
    }
    return _mHSplitView;
}

-(UIButton *)mCancelButton
{
    if (!_mCancelButton) {
        _mCancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_mCancelButton setTitle:@"否" forState:UIControlStateNormal];
        [_mCancelButton setTitleColor: kGlobalRedColor forState:UIControlStateNormal];
        [_mCancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    }
    return _mCancelButton;
}
-(UIButton *)mSureButton
{
    if (!_mSureButton) {
        _mSureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_mSureButton setTitle:@"是" forState:UIControlStateNormal];
        [_mSureButton setTitleColor: kGlobalRedColor forState:UIControlStateNormal];
        [_mSureButton addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    }
    return _mSureButton;
}
@end
